<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Welcome extends CI_Controller {

	function __construct() {

		// Need to run CI_Controller constructor
		parent::__construct();

		// Load the user model
		// This is needed for checking user is logged in and has permission to access the page
		$this->load->model('user');
		$this->load->model('group_message');
	}

	public function index() {

		if ( ! $this->user->is_logged()  ) { redirect('welcome/login'); }
		
		$admin_messages = $this->group_message->getAdminMessages();
		
		$data['page_title'] = "Dashboard";
		$data['title_icon'] = "dashboard";
		$data['main_content'] = "welcome";
		$data['admin_messages'] = $admin_messages;
		
		$this->load->model('groups');
		$group_id = $this->groups->get_students_group($this->session->userdata('user_id'));
		if ($group_id) {
			$data['group_members'] = $this->groups->group_members($group_id[0]['group_id']);
		}

		// Template Settings
		if ($this->user->is_student()) {
			//$data['left_sidebar_icon'] = '';
			$data['left_sidebar_title'] = 'My Group';
		} else {
			$data['left_sidebar_title'] = 'Administration';
		}
		//$data['content_icon'] = '';
		$data['content_title'] = 'Messages';
		//$data['right_sidebar_icon'] = '';
		$data['right_sidebar_title'] = 'My Account';

		if ($this->user->is_student()) {
			$data['left_sidebar'] = 'my_group';
		} else {
			$data['left_sidebar'] = 'admin_links';
		}

		$data['right_sidebar'] = 'my_account';
		
		$this->load->view('template', $data);

	}

	public function login() {

		// Check if the form has been submitted
		if ( $this->input->post() ) {
			// Log user in
			$logged = $this->user->login($this->input->post('qut_id'), $this->input->post('password'));
			// Redirect if logged in
			if ( $logged ) { redirect('welcome'); }
		}

		// Check if user is already logged in and redirect if they are
		if ( $this->user->is_logged()  ) { redirect('welcome'); }

		$data['page_title'] = "Login";

		$this->load->view('login_template', $data);

	}

	public function logout() {
		$this->user->logout();
		redirect('welcome');
	}
}

/* End of file welcome.php */
/* Location: ./application/controllers/welcome.php */